package com.itheima.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.RecommendUser;
import com.itheima.domain.UserLike;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 根据用户id查询今日佳人
     *
     * @param toUserId
     * @return
     */
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        //去mongodb中获取数据
        //构建条件查询对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //设置排序规则
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(1);
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    /**
     * 根据当前用户id去查询推荐的朋友
     * 分页查询
     * @param toUserId
     * @return
     */
    @Override
    public PageResult recommendation(int page, int size, Long toUserId) {
        Query query = Query.query(Criteria.where("toUserId").is(toUserId));
        //先查全部记录数
        long count = mongoTemplate.count(query, RecommendUser.class);
        //实现分页
        query.skip((long) (page - 1) * size)    //跳过这么多条数据
                .limit(size)                    //限制查询这么多条
                .with(Sort.by(Sort.Order.desc("score")));
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        return new PageResult(page,size, (int) count,recommendUsers);
    }

    /**
     * 根据指定用户id和当前用户id查
     * @param userId
     * @param toUserId
     * @return
     */
    @Override
    public RecommendUser getOneTodayBest(Long userId, Long toUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("toUserId").is(toUserId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser == null){
            //没查到就整个默认的
            recommendUser.setUserId(userId);
            recommendUser.setToUserId(toUserId);
            recommendUser.setScore(95D);
        }
        return recommendUser;
    }

    /**
     * 查询推荐列表
     * @param userId
     * @return
     */
    @Override
    public List<RecommendUser> cards(Long userId,int counts) {
        //查询推荐列表需要去除掉已经点过喜欢或者不喜欢的用户
        //查出userId在userLike表中对应的数据
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserIds = userLikes.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());
        //查推荐列表
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserIds);
        //这里是使用统计函数，随机查询
        TypedAggregation<RecommendUser> newAggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria),//指定查询条件
                Aggregation.sample(counts));
        AggregationResults<RecommendUser> aggregate = mongoTemplate.aggregate(newAggregation, RecommendUser.class);
        return aggregate.getMappedResults();
    }


}
